package com.hb.trade.api.routine.controller.center.au;

import cn.hutool.core.util.StrUtil;
import com.hb.core.pojo.center.au.dto.*;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.center.au.vo.OrgCustomConfigVO;
import com.hb.core.pojo.expert.entity.ExpertUser;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.center.UserClient;
import com.hb.trade.client.expert.ExpertClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.AuditStatusEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.*;

/**
 * 企业专属配置信息 前端控制器
 *
 * @author zhaojk
 * @since 2022-04-12
 */
@Slf4j
@Version(1.0)
@Api(tags = {"企业个性化专属配置信息"})
@RequestMapping("/{version}")
@RestController
public class ApiOrgCustomConfigController {

    protected final OrgClient orgClient;
    protected final UserClient userClient;
    protected final ExpertClient expertClient;

    public ApiOrgCustomConfigController(OrgClient orgClient,
                                        UserClient userClient,
                                        ExpertClient expertClient) {
        this.orgClient = orgClient;
        this.userClient = userClient;
        this.expertClient = expertClient;
    }

    /**
     * 更新企业专属配置信息
     *
     * @param configDTO
     * @return
     */
    @ApiOperation(value = "更新企业专属配置信息", notes = "更新企业专属配置信息(提交按钮)")
    @PostMapping("/orgCustomConfig/updateConfig")
    public ResponseDto<?> updateConfig(@RequestBody OrgCustomConfigDTO configDTO) {
        // 参数验证
        String paramsValid = configDTO.paramsValid();
        Long userId = ShiroUtil.getUserId();
        Message<User> userMessage = userClient.getByUserId(userId);
        configDTO.setCreateName(userMessage.getData().getFullName());
        configDTO.setCreateId(userId);

        // orgId为空时, 默认为当前用户
        if (configDTO.getOrgId() == null) {
            configDTO.setOrgId(ShiroUtil.getOrgId());
        }

        if (StrUtil.isNotEmpty(paramsValid)) {
            return ResultUtil.fail(paramsValid);
        }
        // 更新信息
        Message<Boolean> updateResult = orgClient.updateConfig(configDTO);
        if (updateResult.isSuccess()) {
            return ResultUtil.SUCCESS;
        }
        return ResultUtil.msg(updateResult);
    }

    /**
     * 查询企业专属配置信息
     *
     * @param configDTO
     * @return
     */
    @ApiOperation(value = "查询企业专属配置信息", notes = "查询企业专属配置信息(专属配置详情以及表单展示)")
    @PostMapping("/orgCustomConfig/settingInfo")
    public ResponseDto<?> settingInfo(@RequestBody OrgCustomConfigDTO configDTO) {
        // 参数验证
        Long orgId = configDTO.getOrgId();
        if (orgId == null) {
            orgId = ShiroUtil.getOrgId();
            configDTO.setOrgId(orgId);
        }

        OrgCustomConfigVO to;

        // 查询专属配置信息
        Message<OrgCustomConfigDTO> configDTOMessage = orgClient.settingInfo(configDTO);
        if (configDTOMessage.isSuccess()) {
            OrgCustomConfigDTO data = configDTOMessage.getData();
            to = data.to(OrgCustomConfigVO.class);
        } else {
            to = new OrgCustomConfigVO();
        }

        // 私有专家帐号的显示
        Message<ExpertUser> expertUserMsg = expertClient.getByOrgId(orgId);
        if (expertUserMsg.isSuccess()) {
            ExpertUser expertUserMsgData = expertUserMsg.getData();
            to.setExpertUsername(expertUserMsgData.getUsername());
            to.setExpertPassword(expertUserMsgData.getPlaintextPassword());
        }

        return ResultUtil.msg(true, to);
    }

    /**
     * 企业专属配置审核列表
     *
     * @param configDTO
     * @return
     */
    @ApiOperation(value = "企业专属配置审核列表", notes = "企业专属配置审核列表")
    @PostMapping("/orgCustomConfig/auditList")
    public ResponseDto<?> auditList(@RequestBody OrgCustomConfigQueryDTO configDTO) {
        // 查询专属配置信息
        Message<PageResult<OrgCustomConfigDTO>> configDTOMessage = orgClient.auditList(configDTO);
        if (configDTOMessage.isSuccess()) {
            PageResult<OrgCustomConfigDTO> data = configDTOMessage.getData();
            PageResult<OrgCustomConfigVO> pageResult = data.transferTo(OrgCustomConfigVO.class);
            return ResultUtil.msg(true, pageResult);
        }

        return ResultUtil.msg(configDTOMessage);
    }

    /**
     * 审核通过/拒绝
     *
     * @param configDTO
     * @return
     */
    @ApiOperation(value = "审核通过/拒绝", notes = "审核通过/拒绝")
    @PostMapping("/orgCustomConfig/audit")
    public ResponseDto<?> audit(@RequestBody OrgCustomConfigAuditDTO configDTO) {
        Integer auditStatus = configDTO.getAuditStatus();
        if (Enums.equals(AuditStatusEnum.REJECTED, auditStatus)) {
            String reason = configDTO.getAuditReason();
            if (StrUtil.isEmpty(reason)) {
                return ResultUtil.fail("驳回审核必须填写驳回原因");
            }
            if (StrUtil.length(reason) > 1000) {
                return ResultUtil.fail("驳回原因字数限制在1000个字符以内");
            }
        }
        // 查询专属配置信息
        Message<Boolean> configDTOMessage = orgClient.audit(configDTO);
        return ResultUtil.msg(configDTOMessage);
    }

    /**
     * 查看驳回信息
     *
     * @param configDTO
     * @return
     */
    @ApiOperation(value = "查看驳回信息", notes = "查看驳回信息")
    @PostMapping("/orgCustomConfig/rejectInfo")
    public ResponseDto<?> rejectInfo(@RequestBody OrgCustomConfigAuditDTO configDTO) {
        // 查询专属配置信息
        Message<String> configDTOMessage = orgClient.rejectInfo(configDTO);
        return ResultUtil.msg(configDTOMessage);
    }

    /**
     * 通过域名标识查询企业的logo信息
     *
     * @param signKey
     * @return
     */
    @ApiOperation(value = "通过域名标识查询企业的logo信息", notes = "通过域名标识查询企业的logo信息")
    @GetMapping("/orgCustomConfig/displayConfigInfo")
    public ResponseDto<?> displayConfigInfo(Integer signKey) {
        if (signKey == null) {
            signKey = -1;
        }
        Message<OrgCustomConfigDTO> configDTOMessage = orgClient.displayConfigInfo(signKey);
        if (configDTOMessage.isSuccess()) {
            OrgCustomConfigDTO data = configDTOMessage.getData();
            OrgCustomConfigVO to = data.to(OrgCustomConfigVO.class);
            return ResultUtil.msg(true, to);
        }
        return ResultUtil.msg(configDTOMessage);
    }
}
